package com.ybkj.daijia.config;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.SignHelper;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.zhuan.Car;
import com.ybkj.daijia.server.zhuan.Order;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

@Component
public class ZhuanApi {

    /**
     * 创建专车订单
     */
    public static final String CREATE_ORDER = "api/orders/create";
    //  public static final String COUNT_ONDUTYBY_DRIVER = "api/orders/v4/countOnDutyByDriver";
    public static final String COUNT_ONDUTYBY_DRIVER = "api/orders/countOnDutyByDriver";
    /**
     * 根据司机id查询明细
     */
    public static final String DRIVERORDERDETAILVO = "api/orders/driverOrderDetailVo";
    public static final String DRIVERORDERDETAILCOUNT = "api/orders/driverOrderDetailCount";
    /**
     * 补单
     */
    public static final String FILL_ORDER = "api/orders/addmore";
    /**
     * 专车订单汇总
     */
    public static final String COMPOSITIVE = "api/orders/chart";
    /**
     * 查询专车订单
     */

    public static final String QUERY_CAR_BY_IDS = "api/car/findByIds";
    public static final String QUERY_ORDER = "api/orders";
    /**
     * 新查询专车订单接口(只包含列表显示数据).
     */
    public static final String QUERY_ORDERS = "api/orders/queryOrders";
    /**
     * 查询导出专车订单数据
     */
    public static final String QUERY_ORDER_EXPORT = "api/orders/v4up/exportOrders";
    /**
     * 根据服务人员查询执行中的专车订单
     */
    public static final String QUERY_BY_EMPLOY = "api/orders/findByEmployId";
    /**
     * 统计服务人员执行中的未确认专车订单
     */
    public static final String COUNT_BY_EMPLOY = "api/orders/countByEmployId";
    /**
     * 添加订单备注
     */
    public static final String CREATE_ORDERMEMO = "api/orderMemo/remark";
    /**
     * 收回订单
     */
    public static final String CALLBACK = "api/orders/update";
    public static final String PAIDAN = "api/orders/update";
    public static final String ONE_ERRAND = "api/orders/findOne";
    public static final String COMPLETE_TASK = "api/orders/complete";
    /**
     * 查询专车订单(单个)
     */
    public static final String QUERY_ORDER_ONE = "api/orders/findByOrderId";
    /**
     * 查询订单轨迹
     */
    public static final String QUERY_ORDERORBIT = "api/orders/findOrderOrbit";
    /**
     * 查询专车临时订单(单个)
     */
    public static final String QUERY_ORDERTEMP_ONE = "api/orders/getOrderTemp";
    /**
     * 查询专车订单(单个)
     */
    public static final String TEMP_TO_FOREVER = "api/orders/tempToForever";
    /**
     * 查询专车车辆(单个)
     */
    public static final String QUERY_ONE_CAR = "api/car/findByCarId";
    /**
     * 查询专车车辆
     */
    public static final String QUERY_CAR = "api/car/findByParams";
    /**
     * 按条件查询专车车辆
     */
    public static final String QUERY_PARAMS_CARS = "api/car/findByTiaoJian";
    /**
     * 修改车辆上传状态
     */
    public static final String UPDATE_UP_STATUS = "api/car/updateUpStatus";
    /**
     * 取消专车订单
     */
    public static final String CANCEL_ORDER = "api/orders/cancel";
    /**
     * 派单
     */
    public static final String PAIDAN_ORDER = "api/orders/update";
    /**
     * 系统自动派单接口.
     */
    public static final String AUTO_PAIDAN_ORDER = "api/orders/autoPaidan";
    /**
     * 查询专车车辆(根据类型Id)
     */
    public static final String QUERY_CAR_TYPEID = "api/car/findByCarTypeId";
    /**
     * 查询保险到期
     */
    public static final String QUERY_INSURANCETIME = "api/car/findInsuranceTimeOverdue";
    /**
     * 查询年检到期
     */
    public static final String QUERY_OVERHAULTIME = "api/car/findOverhaulTimeOverdue";
    /**
     * 查询车辆类型
     */
    public static final String QUERY_CARTYPE = "api/carType/findCarType";
    /**
     * 通过公司查询车辆类型(数据)
     */
    public static final String QUERY_CARTYPE_COMPANY = "api/carType/findByCompanyCarType";
    /**
     * 统计公司查询车辆类型(数量)
     */
    public static final String COUNT_CARTYPE_COMPANY = "api/carType/countByCompanyCarType";
    public static final String COUNT_CARTYPE_COMPANY_V4UP = "api/orders/v4up/countByCompany";
    /**
     * 查询单个车辆类型
     */
    public static final String QUERY_CARTYPE_ONE = "api/carType/findByCarTypeId";
    /**
     * 删除车辆类型
     */
    public static final String DELETE_CARTYPE = "api/carType/delete";
    /**
     * 删除车辆
     */
    public static final String DELETE_CAR = "api/car/delete";
    /**
     * 修改车辆
     */
    public static final String UPDATE_CAR = "api/car/update";
    /**
     * 修改车辆状态
     */
    public static final String UPDATE_CAR_STATUS = "api/car/updateStatus";
    /**
     * 添加车辆
     */
    public static final String CREATE_CAR = "api/car/create";
    /**
     * 添加车辆类型
     */
    public static final String CREATE_CARTYPE = "api/carType/create";
    /**
     * 修改车辆类型
     */
    public static final String UPDATE_CARTYPE = "api/carType/update";
    /**
     * 查询现有忙碌专车订单数量
     */
    public static final String COUNT_BUSY = "api/orders/countByEmployId";
    /**
     * 查询催单
     */
    public static final String QUERY_CUIDAN = "api/orders/getHastenOrder";
    /**
     * 关闭催单
     */
    public static final String CLOSE_CUIDAN = "api/orders/closeHasten";
    /**
     * 专车订单统计(年)
     */
    public static final String ORDERSTATISTICS = "api/orders/zhuancheDataForYear";
    /**
     * 专车订单统计(月)
     */
    public static final String ORDERSTATISTICSMONTH = "api/orders/zhuancheDataForMonth";
    /**
     * 专车订单统计(日)
     */
    public static final String ORDERSTATISTICSDAY = "api/orders/zhuancheDataForDay";
    public static final String ZHUANEMPLOYORDER = "api/orders/zhuancheEmployOrder";
    public static final String ZHUANHEATMAP = "api/orders/zhuanHeatMap";
    public static final String ZHUANMEMBERORDER = "api/orders/zhuanMemberOrder";
    /**
     * 专车订单类型统计
     */
    public static final String ORDERTYPESTATISTICS = "api/orders/orderFromSource";
    public static final String DRIVERACHIEVEMENT = "api/orders/driverAchievement";
    /**
     * 专车确认订单
     */
    public static final String CONFIRM_TASK = "api/orders/confirmTask";
    /**
     * 专车结算订单
     */
    public static final String FINISH_TASK = "api/orders/finishTask";
    public static final String CREATE_PAYRECORDORDER = "api/payRecordOrder/create";
    public static final String QUERY_PAYRECORDORDER_ONE = "api/payRecordOrder/findOne";
    public static final String TREATMENT_PAYRECORDORDER = "api/payRecordOrder/update";
    /**
     * 按时间查询全部公司的专车订单统计
     */
    public static final String COMPANYORDERSTATISTICS = "api/orders/companyOrder";
    /**
     * 根据客户id查询最近完成订单
     */
    public static final String COUNT_FINISHED_BY_PASSENGER_DATE = "api/orders/countByParam";
    /**
     * 根据客户id查询正在执行的订单数量
     */
    public static final String COUNT_ACTIVED_BY_PASSENGER = "api/orders/queryRunOrder";
    /**
     * 根据客户id查询最近完成订单
     */
    public static final String FIND_LASTEDONE_FINISHED_BY_PASSENGER = "api/orders/findLastOrder";
    /**
     * 根据服务人员id分页查询完成订单
     */
    public static final String QUERY_FINISHED_BY_EMPLOY = "api/orders/queryFinishOrderByEmployId";
    /**
     * 查询服务人员的评价
     */
    public static final String QUERY_ORDEREVIEW = "api/orderReview/findByDriver";
    /**
     * 查询单条评价
     */
    public static final String QUERY_ORDEREVIEW_ONE = "api/orderReview/findOne";
    /**
     * 删除单条评价
     */
    public static final String DELETE_ORDEREVIEW = "api/orderReview/deleteById";
    /**
     * 修改订单金额
     */
    public static final String UPDATE_REALMONEY = "api/orders/updateRealMoney";
    /**
     * 根据passenger查询未支付订单数量
     */
    public static final String COUNT_NOTPAY_BY_PASSENGER = "api/orders/countByPassengerId";
    /**
     * 根据passenger查询新单，派单，接单，执行中数量
     */
    public static final String COUNT_NOTFINISHED_BY_PASSENGER = "api/orders/v4up/countRunOrder";
    /**
     * 根据passenger查询新单、销单、执行中订单数量
     */
    public static final String COUNT_TODAYORDER_BY_PASSENGER = "api/orders/countRunning";
    /**
     * 查询服务人员奖励接口
     */
    public static final String FIND_REWARD = "api/orders/v4/findOrderByEmployId";
    /**
     * 报销
     */
    public static final String BAOXIAO = "api/orders/v4/baoxiao";
    /**
     * 服务人员销单
     */
    public static final String CANCEL = "api/orders/v4/cancel";
    /**
     * 保存车辆类型V4up
     */
    public static final String CREATE_CARTYPE_V4UP = "api/orders/v4up/createCarType";
    /**
     * 公司开通车型,并且设置收费
     */
    public static final String SAVE_CARTYPE_COMPANY = "api/orders/v4up/saveCarTypeCompany";
    /**
     * 修改公司关联车型,并设置收费
     */
    public static final String UPDATE_CARTYPE_COMPANY = "api/orders/v4up/updateCarTypeCompany";
    /**
     * 修改车辆类型V4up
     */
    public static final String UPDATE_CARTYPE_V4UP = "api/orders/v4up/updateCarType";
    /**
     * 删除车型V4up
     */
    public static final String DELETE_CARTYPE_V4UP = "api/orders/v4up/deleteCarType";
    /**
     * 根据分组查询临时订单
     */
    public static final String QUERYBYGROUPID = "api/orders/v4up/queryByGroupId";
    public static final String TEMP_TO_FOREVER_V4UP = "api/orders/v4up/tempToForever";
    public static final String QUERY_CARTYPE_V4UP = "api/orders/v4up/queryCarTypeV4up";
    public static final String QUERY_SERVICE_TYPE = "api/orders/v4up/queryServiceType";
    /**
     * 根据车牌查询车辆
     */
    public static final String QUERY_CAR_BY_NUMBER = "api/orders/v4up/queryCarByNumber";
    public static final String QUERY_PARAMS_CARS_V4UP = "api/orders/v4up/findByTiaoJian";
    public static final String QUERY_CARTYPECOMPANY_V4UP = "api/orders/v4up/queryCarTypeCompany";
    public static final String DELETE_CARTYPECOMPANY_V4UP = "api/orders/v4up/deleteCarTypeCompany";
    public static final String QUERY_CARTYPES_V4UP = "api/orders/v4up/queryCarTypesV4up";
    /**
     * 创建订单
     */
    public static final String CREATE_ORDER_V4UP = "api/orders/v4up/create";
    /**
     * 结算前修改结算时间为当前时间
     */
    public static final String ADD_FINISHTIME = "api/orders/prepare";
    public static final String BATCHCREATE = "api/car/v4/batchCreate";
    /**
     * 专车保险
     */
    public static final String CAR_INSURANCE = "api/car/insertInsurance";
    public static final String CAR_INSURANCE_PAGE = "api/car/finfInsuranceByCarId";
    public static final String DELETET_INSURANCE = "api/car/deletetInsurance";
    public static final String COUNT_CAR = "api/car/countCar";
    public static final String QUERY_CAR_BY_NUMBER_AND_FLIGHTS = "api/orders/v4up/getCarByNumberAndFlights";
    public static final String QUERY_CARTYPE_BY_CARTYPENAME = "api/orders/v4up/getCarTypeByTypeName";
    public static final String QUERY_CAR_TYPE_REL = "api/orders/v4up/findCarTypeRelByCarTypeId";
    public static final String QUERY_CARTYPE_BY_IFTAXI = "api/orders/v4up/getCarTypeByIftaxi";
    //查询能够接预约订单的司机
    public static final String COUNT_BOOK_DRIVER = "api/orders/countBookDriver";
    //查询所有订单都不能接的司机
    public static final String COUNT_BUSY_BOOK_DRIVER = "api/orders/countBusyBookDriver";
    //查询当前司机是否能够接单
    public static final String FIND_DREIVER_BUSY = "api/orders/findDriverBusy";
    //修改订单状态为已到达目的地
    public static final String ARRIVE_DISTINATION_V4 = "api/orders/v4/arriveDestination";
    //APP查询客户的未销单的所有订单.
    public static final String FINDORDERSV4UP = "api/orders/v4up/findOrdersV4up";
    private static final Logger logger = LoggerFactory.getLogger(ZhuanApi.class);
    @Value("${module.zhuan.url}")
    private String zhuanBaseURL;
    @Value("${module.zhuan.appId}")
    private String zhuanAppid;
    @Value("${module.zhuan.appKey}")
    private String zhuanAppkey;
    @Autowired
    @Setter
    private RestTemplate restTemplate;

    public String getZhuanBaseURL() {

        return zhuanBaseURL.endsWith("/") ? zhuanBaseURL : zhuanBaseURL + "/";
    }

    public void setZhuanBaseURL(String errandBaseURL) {
        this.zhuanBaseURL = errandBaseURL;
    }

    public String getZhuanAppid() {
        return zhuanAppid;
    }

    public void setZhuanAppid(String zhuanAppid) {
        this.zhuanAppid = zhuanAppid;
    }

    public String getZhuanAppkey() {
        return zhuanAppkey;
    }

    public void setZhuanAppkey(String zhuanAppkey) {
        this.zhuanAppkey = zhuanAppkey;
    }

    public static final String GET_OVERTIME_ORDERS = "api/orders/v4up/getOverTimeOrder";

    public String get(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appKey", setting.getSecKey());

        String result = null;

        try {
            result = restTemplate.getForObject(apiurl, String.class, kv);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    public String post(String apiurl, MultiValueMap<String, String> kv, Setting setting) {

        kv.add("appKey", setting.getSecKey());

        Set<Entry<String, List<String>>> entries = kv.entrySet();

        Map<String, String> params = new HashMap<String, String>();
        for (Entry<String, List<String>> entry : entries) {
            List<String> values = entry.getValue();
            params.put(entry.getKey(), CollectionUtils.isEmpty(values) ? null : values.get(0));
        }

        RestTemplate rest = new RestTemplate();

        String result = null;
        try {
            result = rest.postForObject(apiurl, kv, String.class);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public String put(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appKey", setting.getSecKey());

        String result = null;
        try {
            restTemplate.put(apiurl, null, kv);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = "false";
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = "false";
        }
        return result;

    }

    public String delete(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appKey", setting.getSecKey());

        String result = null;

        try {
            restTemplate.delete(apiurl, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return result;
    }

    public Car findCar(Long id, Setting setting) {
        StringBuilder builder = new StringBuilder();
        builder.append(getZhuanBaseURL() + QUERY_ONE_CAR + ".json?carId={carId}&appKey={appKey}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("carId", String.valueOf(id));
        Car car = null;
        String result = get(builder.toString(), kv, setting);
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("null")) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    car = gson.fromJson(data.toString(), Car.class);
                }
            }
        }
        return car;
    }

    public String updateCarStatus(Long carId, Integer status, Setting setting) {
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("carId", String.valueOf(carId));
        kv.add("status", String.valueOf(status));
        String result = null;
        try {
            result = post(getZhuanBaseURL() + ZhuanApi.UPDATE_CAR_STATUS, kv, setting);
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
        }

        return result;
    }

    public long countFinishedByPassengerAndFinishTime(Long passengerId, String orderFromSources,
        Date startDate, Date endDate, Setting setting) {
        long finishedTimes = 0;

        StringBuilder builder = new StringBuilder();
        builder.append(
            getZhuanBaseURL() + ZhuanApi.COUNT_FINISHED_BY_PASSENGER_DATE + "?appKey={appKey}");
        Map<String, String> kv = new HashMap<String, String>();

        kv.put("passengerId", String.valueOf(passengerId));
        builder.append("&passengerId={passengerId}");

        if (StringUtils.isNotBlank(orderFromSources)) {
            kv.put("orderFromSources", String.valueOf(orderFromSources));
            builder.append("&orderFromSources={orderFromSources}");
        }

        kv.put("startTime", String.valueOf(startDate.getTime()));
        builder.append("&startTime={startTime}");

        if (null != endDate) {
            kv.put("endTime", String.valueOf(endDate.getTime()));
            builder.append("&endTime={endTime}");
        }

        String result = get(builder.toString(), kv, setting);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                finishedTimes = jo.getLong("data");
            }
        }
        return finishedTimes;
    }

    public Result<Order> selectLastedOneFinishedByPassenger(Long passengerId, Setting setting) {

        Result<Order> result = new Result<Order>();
        result.setSuccess(false);

        Order order = null;

        StringBuilder builder = new StringBuilder();
        builder.append(getZhuanBaseURL() + ZhuanApi.FIND_LASTEDONE_FINISHED_BY_PASSENGER
            + "?passengerId={passengerId}&appKey={appKey}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("passengerId", String.valueOf(passengerId));
        String resultStr = get(builder.toString(), kv, setting);

        if (null != resultStr) {
            JSONObject jo = new JSONObject(resultStr);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Object object = jo.get("data");
                if (null != object && !object.toString().equals("null")) {
                    JSONObject data = jo.getJSONObject("data");
                    Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                    order = gson.fromJson(data.toString(), Order.class);
                    result.setResult(order);
                }
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setMessage(jo.getString("message"));
            }
        }
        return result;
    }

    public long countActiveByPassenger(Long passengerId, Setting setting) {
        long activedTimes = 0;

        StringBuilder builder = new StringBuilder();
        builder
            .append(getZhuanBaseURL() + ZhuanApi.COUNT_ACTIVED_BY_PASSENGER + "?appKey={appKey}");
        Map<String, String> kv = new HashMap<String, String>();

        kv.put("passengerId", String.valueOf(passengerId));
        builder.append("&passengerId={passengerId}");

        String result = get(builder.toString(), kv, setting);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                activedTimes = jo.getLong("data");
            }
        }
        return activedTimes;
    }

    public String postMultiValueMap(String url, MultiValueMap<String, String> kv, Setting setting) {

        kv.add("appKey", setting.getSecKey());

        Set<Entry<String, List<String>>> entries = kv.entrySet();

        Map<String, String> params = new HashMap<String, String>();
        for (Entry<String, List<String>> entry : entries) {
            List<String> values = entry.getValue();
            params.put(entry.getKey(), CollectionUtils.isEmpty(values) ? null : values.get(0));
        }

        String sign = SignHelper.sign(SignHelper.buildSignMessage(params), zhuanAppkey);

        kv.add("sign", sign);
        kv.add("appId", zhuanAppid);

        logger.info("zhuanAppid:{}", zhuanAppid);

        RestTemplate rest = new RestTemplate();

        String result = null;

        try {
            result = rest.postForObject(url, kv, String.class);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public Result<Order> baoxiao(Long orderId, BigDecimal money, Setting setting) {

        Result<Order> vo = new Result<Order>();

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();

        kv.add("orderId", String.valueOf(orderId));
        kv.add("baoxiaoMoney", String.valueOf(money));

        String result = post(getZhuanBaseURL() + ZhuanApi.BAOXIAO, kv, setting);

        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                vo.setSuccess(true);
            } else {
                String message = jo.getString("message");
                vo.setMessage(message);
            }
        } else {
            vo.setMessage("内部服务器错误");
        }

        return vo;

    }

    public Result<Order> findOneOrder(Long orderId, Setting setting) {

        Result<Order> vo = new Result<Order>();

        StringBuilder builder = new StringBuilder();
        builder.append(
            getZhuanBaseURL() + ZhuanApi.QUERY_ORDER_ONE + "?orderId={orderId}&appKey={appKey}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("orderId", String.valueOf(orderId));
        String result = get(builder.toString(), kv, setting);
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                Order order = gson.fromJson(data.toString(), Order.class);
                vo.setResult(order);
            }
        }
        return vo;

    }

    public Result<Order> addFinishTime(Long orderId, Setting setting) {

        Result<Order> vo = new Result<Order>();

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();

        kv.add("orderId", String.valueOf(orderId));

        String result = post(getZhuanBaseURL() + ZhuanApi.ADD_FINISHTIME, kv, setting);

        if (StringUtils.isNotBlank(result)) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                Boolean data = jo.getBoolean("data");
                vo.setSuccess(data);
                return vo;
            } else {
                String message = jo.getString("message");
                vo.setMessage(message);
            }
        } else {
            vo.setMessage("内部服务器错误");
        }

        return vo;

    }

    /**
     * APP查询客户的未销单的所有订单.
     *
     * @param passengerId
     * @param page
     * @param rows
     * @param status
     * @param setting
     * @return
     */
    public List<Order> findOrdersV4up(Long passengerId, Integer page, Integer rows, Integer status,
        Setting setting) {
        List<Order> orders = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        builder.append(getZhuanBaseURL() + ZhuanApi.FINDORDERSV4UP
            + "?passengerId={passengerId}&appKey={appKey}&" +
            "page={page}&rows={rows}&status={status}");
        Map<String, String> kv = new HashMap<String, String>();
        kv.put("passengerId", String.valueOf(passengerId));
        kv.put("page", String.valueOf(page));
        kv.put("rows", String.valueOf(rows));
        kv.put("status", String.valueOf(status));
        String result = get(builder.toString(), kv, setting);
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                JSONArray content = data.getJSONArray("rows");
                Gson gson = new GsonBuilder().setDateFormat(com.ybkj.daijia.constant.DateConstant.DATE_TIME_FORMATE).create();
                orders = gson.fromJson(content.toString(), new TypeToken<List<Order>>() {
                }.getType());
                return orders;

            }
        }
        return orders;
    }

    /**
     * 已到达目的地
     *
     * @param orderId  订单id
     * @param operator 操作人
     * @param appKey   系统key
     * @return ResultTransport
     */
    public ResultTransport arriveDistination(Long orderId, String operator, String appKey,
        Setting setting) {
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("orderId", String.valueOf(orderId));
        kv.add("operator", operator);
        kv.add("appKey", appKey);
        String result = restTemplate
            .postForObject(getZhuanBaseURL() + ARRIVE_DISTINATION_V4, kv, String.class);
        ResultTransport parseObject = JSON.parseObject(result, ResultTransport.class);

        return parseObject;
    }
}
